package org.linlinjava.litemall.admin.web.distribution;

import org.linlinjava.litemall.admin.annotation.LoginAdmin;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.DisProfitParam;
import org.linlinjava.litemall.db.domain.DisProfitRecord;
import org.linlinjava.litemall.db.service.distribution.DisProfitParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 参数设置控制器
 *
 * @author huangpu
 * @Date 2018-04-06 11:33:32
 */
@Controller
        @RequestMapping("/admin/disProfitParam")
public class DisProfitParamController extends BaseController {


    @Autowired
    DisProfitParamService disProfiParamService;

    /**
     * 获取参数设置列表
     */
    /**
     * 获取交易列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@LoginAdmin Integer adminId,
                       String goodsId,
                       @RequestParam(value = "page", defaultValue = "1") Integer page,
                       @RequestParam(value = "limit", defaultValue = "10") Integer limit
    ) {
        if (adminId == null) {
            return ResponseUtil.fail401();
        }
        List<DisProfitParam> memList = disProfiParamService.querySelective(goodsId, page, limit);
        int total = disProfiParamService.countSeletive(goodsId, page, limit);
        Map<String, Object> data = new HashMap<>();
        data.put("total", total);
        data.put("items", memList);
        return ResponseUtil.ok(data);
    }

    /**
     * 新增参数设置
     */
    @RequestMapping(value = "/create")
    @ResponseBody
    public Object add(@RequestBody DisProfitParam param) {
        disProfiParamService.save(param);
        return ResponseUtil.ok(param);
    }


    /**
     * 删除参数设置
     */
    @DeleteMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer id) {
        disProfiParamService.delete(id);
        return ResponseUtil.ok();
    }


    /**
     * 修改参数设置
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(@RequestBody DisProfitParam info) {
        try {
            disProfiParamService.update(info);
        } catch (Exception e) {
            return ResponseUtil.fail(500, "更新失败，" + e.getMessage());
        }
        return ResponseUtil.ok(info);
    }

    /**
     * 参数设置详情
     */
    @RequestMapping(value = "/detail")
    @ResponseBody
    public Object detail() {
        return null;
    }
}
